﻿using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Threading;
using System.Text.RegularExpressions;

public partial class public_html_NewSite_CMSV2_GamesAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ReviewerMySql SQL = new ReviewerMySql();
        
        if (!IsPostBack)
        {
            OdbcDataReader MyReader = SQL.ExecuteReader("SELECT * FROM V2_GloGenres ORDER BY Navn");
            while (MyReader.Read())
            {
                ListGenre.Items.Add(new ListItem(MyReader[1].ToString(), MyReader[0].ToString()));
            }
            OdbcDataReader BasisReader = SQL.ExecuteReader("SELECT * FROM V2_Spil WHERE id =" + Request.QueryString["id"].ToString());
            string SpilID = "";
            string Format = "";
            while (BasisReader.Read())
            {
                string Dato = BasisReader["ReleaseDato"].ToString();
                SpilID = BasisReader["SpilID"].ToString();
                Format = BasisReader["Format"].ToString();
                TxtID.Text = BasisReader["SpilID"].ToString();
                TxtTitel.Text = BasisReader["Titel"].ToString();
                TxtUdgiver.Text = BasisReader["Udgiver"].ToString();
                TxtUdvikler.Text = BasisReader["Udvikler"].ToString();
                TxtMulti.Text = BasisReader["Multi"].ToString();
                if (Dato.Length > 2)
                {
                    string[] SplitDate = Regex.Split(Dato.ToString(), "-");
                    string[] Year = Regex.Split(SplitDate[2], " ");

                    TxtDato.Text = Year[0] + "-" + SplitDate[1] + "-" + SplitDate[0];// +"-" + SplitDate[1] + "-" + SplitDate[0]; 
                }
                lblTitel.Text = Request.QueryString["Titel"].ToString();
                string FilePath = Server.MapPath("../SpilTop/" + TxtID.Text + ".jpg");
                FileInfo FileInf = new FileInfo(FilePath);
                if (FileInf.Exists)
                {
                    imgTopBilled.ImageUrl = "../SpilTop/" + TxtID.Text + ".jpg";
                }
                else
                {
                    imgTopBilled.Visible = false;
                }
                for (int i = 0; i < ListGenre.Items.Count; i++)
                {
                    if (ListGenre.Items[i].Value.ToString() == BasisReader["Genre"].ToString())
                    {
                        ListGenre.Items[i].Selected = true;
                    }
                }
            }

            OdbcDataReader reader = SQL.ExecuteReader("SELECT * FROM V2_Screenshots WHERE SpilID = '" + SpilID + "' AND Format = '" + Format + "'");

            if (File.Exists(Server.MapPath("../covers/" + SpilID + "_" + Format + ".jpg")))
            {
                DivScreens.InnerHtml += "<b>Cover:</b><br /><img width=\"120px\" src=\"../covers/" + SpilID + "_" + Format + ".jpg\"><br/><br/>";
            }
            else
            {
                DivScreens.InnerHtml += "Der er endnu intet cover til dette format<br/><br/>";
            }
            
            int z = 0;
            DivScreens.InnerHtml += "<table border=\"0\"><tr>";
            
            while (reader.Read())
            {
                if (z == 5)
                {
                    z = 0;
                    DivScreens.InnerHtml += "</tr><tr>";
                }
                DivScreens.InnerHtml += "<td><img width=\"130px\" src=\"../screenshots/" + reader["Filnavn"].ToString() + "\"></td>";
                z++;
            }
            DivScreens.InnerHtml += "</tr></table>";
            DivScreens.InnerHtml += "<br /><br /><a href=\"GamesScreenshot.aspx?id=" + SpilID + "&titel=" + Request.QueryString["titel"] + "\">Upload Screenshots</a>";
            
            int OtherGamesCounter = 0;

            OdbcDataReader OtherReader = SQL.ExecuteReader("SELECT V2_Spil.*, V2_GloFormat.Navn AS GloFormat FROM V2_Spil JOIN V2_GloFormat ON V2_GloFormat.id = V2_Spil.Format WHERE Format <> '" + Format + "' AND V2_Spil.SpilID = '" + SpilID + "'");
            while(OtherReader.Read()) 
            {
                if (OtherGamesCounter == 0)
                {
                    DivAndreSpil.InnerHtml =  "<b>Andre formater</b><br />";
                }
                DivAndreSpil.InnerHtml += "<a href=\"?id=" + OtherReader["id"].ToString() + "&titel=" + OtherReader["Titel"].ToString() + " (" + OtherReader["GloFormat"].ToString() + ")\">" + OtherReader["GloFormat"].ToString() + "</a><br />";
                OtherGamesCounter++;
            }
        }
    }

    protected void SaveVideo(object sender, EventArgs e)
    {
        ReviewerMySql SQL = new ReviewerMySql();
        SQL.ExecuteNonQuery("INSERT INTO V2_SpilVideo(Titel, Link, Tekst, SpilID) VALUES('" + txtVidOver.Text + "','" + txtVidLink.Text + "','" + txtVidDesc.Text + "','" + TxtID.Text + "')");
    }

    protected void BtmUpdateGame_Click(object sender, EventArgs e)
    {
        ReviewerMySql SQL = new ReviewerMySql();
        SQL.ExecuteNonQuery("UPDATE V2_Spil SET Titel = '" + TxtTitel.Text + "', Udgiver = '" + TxtUdgiver.Text + "', Udvikler = '" + TxtUdvikler.Text + "', Multi = '" + TxtMulti.Text + "', ReleaseDato = '" + TxtDato.Text + "', Genre = " + ListGenre.SelectedValue + " WHERE ID = " + Request.QueryString["id"]);
        //Response.Redirect("Message.aspx?MSG= Spillet blev opdateret");
        Label GlobalLabel = (Label)Master.FindControl("lblGlb");
        GlobalLabel.Text = "Spillet blev Redigeret";

        //Reviewer.Games.Games.LastUpdated(Txt);
    }

    protected void BtmSaveGame_Click(object sender, EventArgs e)
    {
        ReviewerMySql SQL = new ReviewerMySql();

        for (int i = 0; i < ListFormat.Items.Count; i++)
        {
            if (ListFormat.Items[i].Selected)
            {
                string Dato = "";
                string Multi = "";
                SQL.ExecuteNonQuery("INSERT INTO V2_Spil (ID, SpilID, Titel, Udgiver, Udvikler, Genre, Multi, Format) VALUES (NULL , '" + TxtID.Text + "', '" + TxtTitel.Text + "', '" + TxtUdgiver.Text + "', '" + TxtUdvikler.Text + "', '" + ListGenre.SelectedValue + "', '" + TxtMulti.Text + "' , '" + ListFormat.Items[i].Value + "')");                 
            }
        }

        Label GlobalLabel = (Label)Master.FindControl("lblGlb");
        GlobalLabel.Text = "Spillet blev Oprettet";
        
        //Response.Redirect("Message.aspx?MSG= Spil(lene) blev oprettet");
    }

    protected void BtmSlet_Click(object sender, EventArgs e)
    {
        ReviewerMySql SQL = new ReviewerMySql();
        OdbcDataReader SletReader = SQL.ExecuteReader("SELECT * FROM V2_Spil WHERE id = " + Request.QueryString["id"].ToString());
        if (SletReader.Read())
        {
            string Format = SletReader["Format"].ToString();
            string SpilID = SletReader["SpilID"].ToString();
            string ID = SletReader["id"].ToString();

            SQL.ExecuteNonQuery("DELETE FROM V2_Screenshots WHERE SpilID = '" + SpilID + "' AND Format = '" + Format + "'");
            SQL.ExecuteNonQuery("DELETE FROM V2_Anmeldelse WHERE SpilID = " + Format + "");
            SQL.ExecuteNonQuery("DELETE FROM V2_Spil WHERE id = " + ID);

            Response.Redirect("Message.aspx?MSG= Spillet blev slettet");
        }
    }
    protected void btmSaveInfoBilled_Click(object sender, EventArgs e)
    {
        if (FileUpTopBilled.HasFile)
        {
            ReviewerMySql SQL = new ReviewerMySql();
            string Path = Server.MapPath("../SpilTop/");
            FileUpTopBilled.SaveAs(@Path + TxtID.Text + ".jpg");
            SQL.ExecuteNonQuery("INSERT INTO V2_SpilTopBilled(SpilID, Filnavn, TextInLogo, TextColor) VALUES('" + TxtID.Text + "','" + TxtID.Text + ".jpg','" + chkLogoTitel.Checked.ToString() + "','" + ddlTextColor.SelectedValue.ToString() + "')");
        }
    }
}
